#!/bin/bash

echo "--------------------Notice:-------------------------------"
echo "本脚本将会安装所有仿真环境，适合刚刚安装完系统的用户"
echo "注意：本脚本会将Home目录下catkin_ws重命名为catkin_ws_back --"
echo "----------designed by rocket 2024.10.22-------------v3.1---"
echo "gazebo version:  11"
echo "PX4 version:   1.13"
echo "按下回车建开始安装"

read -n 1

echo "正在自动更换 清华源"
sudo mv /etc/apt/sources.list /etc/apt/sources.list.back
sudo sh -c 'echo  "deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal-updates main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal-updates main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal-backports main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal-backports main restricted universe multiverse" > /etc/apt/sources.list'

sudo apt update


echo "正在复制文件，请勿退出..."
folder="$HOME/PX4_Firmware"
# 判断文件夹是否存在
if [ -d "$folder" ]; then
    echo "PX4已经存在"
else
    echo "正在复制PX4"
    cp -r ./sources/PX4_Firmware_13 ~/
    mv ~/PX4_Firmware_13 ~/PX4_Firmware
fi
find ~/PX4_Firmware/ -type f -name "*.sh" -exec chmod +x {} \;
folder="$HOME/.gazebo/models"
# 判断文件夹是否存在
if [ -d "$folder" ]; then
    echo "models已经存在"
else
    echo "正在复制models"
    mkdir -p ~/.gazebo
    cp -r ./sources/models ~/.gazebo
fi


sudo cp -r ./sources/GeographicLib  /usr/share




# ROS

sudo sh -c '. /etc/lsb-release && echo "deb http://mirrors.ustc.edu.cn/ros/ubuntu/ $DISTRIB_CODENAME main" > /etc/apt/sources.list.d/ros-latest.list'
sudo apt-key adv --keyserver 'hkp://keyserver.ubuntu.com:80' --recv-key C1CF6E31E6BADE8868B172B4F42ED6FBAB17C654
sudo apt update
sleep 1; 
sudo apt install ros-noetic-desktop-full -y
echo "source /opt/ros/noetic/setup.bash" >> ~/.bashrc
sudo apt install python3-rosinstall python3-rosinstall-generator python3-wstool -y
sudo apt-get install python3-pip -y
pip3 config set global.index-url https://pypi.tuna.tsinghua.edu.cn/simple
sudo pip3 install 6-rosdep
sudo 6-rosdep
sudo rosdep init
sudo rosdep update
source /opt/ros/noetic/setup.bash
echo "ROS安装完毕！！！"



# deps
sudo apt install ninja-build exiftool ninja-build protobuf-compiler libeigen3-dev genromfs xmlstarlet libgstreamer1.0-dev libgstreamer-plugins-base1.0-dev python3-pip gawk -y



# pip2 install pandas jinja2 pyserial cerberus pyulog==0.7.0 numpy toml pyquaternion empy pyyaml 

pip3 install packaging numpy empy toml pyyaml jinja2 pyargparse kconfiglib jsonschema future
sudo apt-get install python3-catkin-tools -y
sudo apt install git -y



# gazebo
sudo apt-get remove -f gazebo* -y
sudo apt-get remove -f libgazebo* -y
sudo apt-get remove -f ros-noetic-gazebo* -y
source ~/.bashrc

sudo sh -c 'echo "deb http://packages.osrfoundation.org/gazebo/ubuntu-stable `lsb_release -cs` main" > /etc/apt/sources.list.d/gazebo-stable.list'
wget https://packages.osrfoundation.org/gazebo.key -O - | sudo apt-key add -
sudo apt-get update
sudo apt-get install -f gazebo11 -y
sudo apt-get install -f libgazebo11-dev -y

# ROS packages
sudo apt-get install ros-noetic-moveit-msgs ros-noetic-object-recognition-msgs ros-noetic-octomap-msgs ros-noetic-camera-info-manager ros-noetic-control-toolbox ros-noetic-polled-camera ros-noetic-controller-manager ros-noetic-transmission-interface ros-noetic-joint-limits-interface -y



# XTDrone download
cd ~
git clone https://gitee.com/robin_shaun/XTDrone.git

# MavROS
sudo apt install ros-noetic-mavros ros-noetic-mavros-extras -y


#初始化工作空间
cd ~
mv  ~/catkin_ws ~/catkin_ws_back
mkdir -p ~/catkin_ws/src
mkdir -p ~/catkin_ws/scripts
cd ~/catkin_ws 
source /opt/ros/noetic/setup.bash
catkin init 
sleep 1; 
catkin build

#文件复制
cd ~/catkin_ws
cp -r ~/XTDrone/sitl_config/gazebo_ros_pkgs src/

#使用j4避免内存爆炸，如果性能足够可以开多点线程
catkin build -j4




# PX4

cd ~/
cd PX4_Firmware
echo 'source ~/catkin_ws/devel/setup.bash' >>~/.bashrc
echo 'source ~/PX4_Firmware/Tools/setup_gazebo.bash ~/PX4_Firmware/ ~/PX4_Firmware/build/px4_sitl_default' >>~/.bashrc
echo 'export ROS_PACKAGE_PATH=$ROS_PACKAGE_PATH:~/PX4_Firmware' >>~/.bashrc
echo 'export ROS_PACKAGE_PATH=$ROS_PACKAGE_PATH:~/PX4_Firmware/Tools/sitl_gazebo' >>~/.bashrc

#Xtdrone file copy
cd ~/XTDrone
git checkout 1_13_2 
git submodule update --init --recursive
cp sitl_config/init.d-posix/* ~/PX4_Firmware/ROMFS/px4fmu_common/init.d-posix/
cp -r sitl_config/launch/* ~/PX4_Firmware/launch/
cp sitl_config/worlds/* ~/PX4_Firmware/Tools/sitl_gazebo/worlds/
cp sitl_config/gazebo_plugin/gimbal_controller/gazebo_gimbal_controller_plugin.cpp ~/PX4_Firmware/Tools/sitl_gazebo/src
cp sitl_config/gazebo_plugin/gimbal_controller/gazebo_gimbal_controller_plugin.hh ~/PX4_Firmware/Tools/sitl_gazebo/include
cp sitl_config/gazebo_plugin/wind_plugin/gazebo_ros_wind_plugin_xtdrone.cpp ~/PX4_Firmware/Tools/sitl_gazebo/src
cp sitl_config/gazebo_plugin/wind_plugin/gazebo_ros_wind_plugin_xtdrone.h ~/PX4_Firmware/Tools/sitl_gazebo/include
cp sitl_config/CMakeLists.txt ~/PX4_Firmware/Tools/sitl_gazebo
cp -r sitl_config/models/* ~/PX4_Firmware/Tools/sitl_gazebo/models/ 
cd ~/.gazebo/models/
rm -r stereo_camera/ 3d_lidar/ 3d_gpu_lidar/ hokuyo_lidar/




source ~/catkin_ws/devel/setup.bash
source ~/PX4_Firmware/Tools/setup_gazebo.bash ~/PX4_Firmware/ ~/PX4_Firmware/build/px4_sitl_default
export ROS_PACKAGE_PATH=$ROS_PACKAGE_PATH:~/PX4_Firmware
export ROS_PACKAGE_PATH=$ROS_PACKAGE_PATH:~/PX4_Firmware/Tools/sitl_gazebo

echo "全部准备工作完成，开始度劫！！！"
sleep 1;

cd ~/PX4_Firmware
rm -r build
#防止报错
rm ~/PX4_Firmware/Tools/sitl_gazebo/models/typhoon_h480/typhoon_h480.sdf
chmod +x ~/PX4_Firmware/Tools/check_submodules.sh
chmod +x ~/PX4_Firmware/Tools/sitl_run.sh
pip3 install --user pyros-genmsg

make px4_sitl_default gazebo
